package com.hup.utils.io.batch.function;

import com.hup.utils.io.batch.BaseBatch;
import com.hup.utils.io.batch.SimpleBatchResp;
import lombok.extern.log4j.Log4j2;

import java.util.ArrayList;

/**
 * @author hugan
 * @date 2020/1/22
 */
@Log4j2
public class NetStop implements BaseBatch<SimpleBatchResp> {

    private final String serviceName;

    public NetStop(String serviceName) {
        this.serviceName = serviceName;
    }

    @Override
    public String cmd() {
        return "net stop \"" + serviceName + "\"";
    }

    @Override
    public String desc() {
        return "停止[" + serviceName + "]服务";
    }

    @Override
    public SimpleBatchResp parse(ArrayList<String> lstResp) {
        log.debug("resp.size={}", lstResp.size());
        if (lstResp.isEmpty()) {
            return SimpleBatchResp.fail(this, "无返回信息");
        }
        //log.info("[{}]", TextUtil.getLineWrap(lstResp));

        //权限不足
        if (lstResp.get(0).contains("服务名无效")) {
            return SimpleBatchResp.fail(this, lstResp.get(0));
        }

        //权限不足
        if (lstResp.get(0).contains("错误")) {
            return SimpleBatchResp.fail(this, lstResp.get(2));
        }

        //没有启动,不需权限
        if (lstResp.get(0).contains("没有启动")) {
            return SimpleBatchResp.success();
        }

        if (lstResp.size() < 2) {
            return SimpleBatchResp.fail(this, lstResp);
        }

        //无法停止??
        if (lstResp.get(1).contains("无法停止")) {
            return SimpleBatchResp.fail(this, lstResp.get(1));
        }

        //必须含有[成功停止]标志,才是成功
        if (lstResp.get(1).contains("成功停止")) {
            return SimpleBatchResp.success();
        }

        return SimpleBatchResp.fail(this, lstResp);
    }

}
